<?php
class Menu extends Admincontroll{
    //put your code here
    private $admin_menulogic;
    public function __construct() {
        $this->admin_menulogic=  $this->load("admin_menu");
    }
    public function index(){
        $db=$this->admin_menulogic->select("id,parent_id as pid,base_name as name"," parent_id=0");
        Temp::assign("a", "add");
        Temp::assign("list", $db);
        Temp::display();
    }
    public function delete(){
        $id=isset($_REQUEST["id"])?intval($_REQUEST["id"]):0;
        if(!$id){
            return $this->showmessage("参数异常！","错误信息");
        }
        $menuinfo=  $this->admin_menulogic->get_one("*","id={$id}");
        if($menuinfo["has_sun"]){
            return $this->showmessage("改分类底下有子分类,请删除子分类后在删除该分类", "错误信息", "?menu/menulist");
        }else{
            
            $res=$this->admin_menulogic->delete("id={$id}");
            $count=  $this->admin_menulogic->get_count("parent_id={$menuinfo["parent_id"]}");
            if($count<1){
                 $parent_info=array(
                "has_sun"=>0
            );
            $this->admin_menulogic->update($parent_info,"id={$menuinfo["parent_id"]}");
            }
            if($res){
                 return $this->showmessage("删除成功", "成功信息", "?menu/menulist");
            }
        }
    }
    public function edit(){
        $id=isset($_REQUEST["id"])?intval($_REQUEST["id"]):0;
        if(!$id){
            return $this->showmessage("参数异常！","错误信息");
        }
        
      
        if(isset($_REQUEST["submit"])){
            $oldmenu=  $this->admin_menulogic->get_one("*","id={$id}");
           
            $base_name=  isset($_REQUEST["base_name"])?$_REQUEST["base_name"]:"";
            $parent_id=isset($_REQUEST["parent_id"])?intval($_REQUEST["parent_id"]):0;
            $c= isset($_REQUEST["c"])?$_REQUEST["c"]:"";
            $a= isset($_REQUEST["a"])?$_REQUEST["a"]:"";
            $is_display=isset($_REQUEST["is_display"])?$_REQUEST["is_display"]:0;
            $sort=isset($_REQUEST["sort"])?$_REQUEST["sort"]:0;
            $menu_data=array(
            "base_name"=>  Common::strEscape($base_name),
            "parent_id"=>$parent_id,
            "c"=> Common::strEscape($c),
            "a"=> Common::strEscape($a),
            "is_display"=>$is_display,
            "sort"=>$sort
        );
        if($parent_id!=0){
            $parent=$this->admin_menulogic->get_one("*","id={$parent_id}");
          
            if($parent["has_sun"]==0){
                $this->admin_menulogic->update("has_sun=1","id={$parent_id}");
            }
            $menu_data["level"]=  intval($parent["level"])+1;
        }
        $res=$this->admin_menulogic->update($menu_data,"id={$id}");
        $count=  $this->admin_menulogic->get_count("parent_id={$oldmenu["parent_id"]}");
        if($count<1){
            $parent_info=array(
                "has_sun"=>0
            );
            $this->admin_menulogic->update($parent_info,"id={$oldmenu["parent_id"]}");
        }
        if($res){
            return $this->showmessage("更新成功", "成功信息", "?menu/menulist");
        }
        
        }else{
            $menu=  $this->admin_menulogic->select("*","id={$id}");
            $data=array();
            $this->get_parent($menu[0]["parent_id"], $data);
            Temp::assign("pinfo", $data);
            Temp::assign("menu", $menu[0]);
        }
        Temp::assign("id", $id);
        Temp::display();
    }
    private function get_parent($pid,&$data){
        $menu=  $this->admin_menulogic->select("*","id={$pid}");
        $tmp=array();
        if(!$menu){
            $data[]=0;
            return ;
        }
        if($menu[0]["parent_id"]!=0){
            $this->get_parent($menu[0]["parent_id"], $data);
            $menut=  $this->admin_menulogic->select("*","parent_id={$menu[0]["parent_id"]}");
            $tmp["selected"]=$pid;
            $tmp["data"]=$menut;
            $data[]=$tmp;
           
        }else{
            $menu=  $this->admin_menulogic->select("*","parent_id=0");
            $tmp["selected"]=$pid;
            $tmp["data"]=$menu;
            $data[]=$tmp;
            
        }
    }
    public function public_get_children(){
        $parent_id=isset($_REQUEST["parent_id"])?intval($_REQUEST["parent_id"]):0;
        $noid="";
        if(isset($_REQUEST["curid"])){
            $noid=" and id!={$_REQUEST["curid"]}";
        }
        $db=$this->admin_menulogic->select("id,parent_id as pid,base_name as name"," parent_id={$parent_id} {$noid}");
        Temp::assign("list", $db);
        Temp::assign("parent_id", $parent_id);
        Temp::display();
    }
    public function menulist(){
         $db=$this->admin_menulogic->select("id,parent_id as pId,base_name as name","1=1 order by sort desc");
         Temp::assign("menulist", json_encode($db));
         Temp::display();
    }
    //通过父类id添加子分类
    public function addbypid(){
         $pid= isset($_REQUEST["pid"])?$_REQUEST["pid"]:0;
         if($pid){
             $data=array();
             $this->get_parent($pid, $data);
             Temp::assign("pinfo", $data);
             Temp::display();
         }else{
             $this->add();
         }
    }

    public function add(){
        $base_name=  isset($_REQUEST["base_name"])?$_REQUEST["base_name"]:"";
        $parent_id=isset($_REQUEST["parent_id"])?intval($_REQUEST["parent_id"]):0;
        $c= isset($_REQUEST["c"])?$_REQUEST["c"]:"";
        $a= isset($_REQUEST["a"])?$_REQUEST["a"]:"";
        $is_display=isset($_REQUEST["is_display"])?$_REQUEST["is_display"]:0;
        $sort=isset($_REQUEST["sort"])?$_REQUEST["sort"]:0;
        if(!$base_name){
            return $this->showmessage("请输入菜单名称！","错误信息");
        }
        if(!$c){
            return $this->showmessage("请输入控制器名称！","错误信息");
        }
        if(!$a){
            return $this->showmessage("请输入方法名称！","错误信息");
        }
        if($parent_id<0){
            return $this->showmessage("请选择分类！","错误信息");
        }
        $menu_data=array(
            "base_name"=>  Common::strEscape($base_name),
            "parent_id"=>$parent_id,
            "c"=> Common::strEscape($c),
            "a"=> Common::strEscape($a),
            "is_display"=>$is_display,
            "sort"=>$sort
        );
        $logic= $this->admin_menulogic;
        if($parent_id!=0){
            $parent=$logic->select("*","id={$parent_id}");
          
            if($parent[0]["has_sun"]==0){
                $logic->update("has_sun=1","id={$parent_id}");
            }
            $menu_data["level"]=  intval($parent[0]["level"])+1;
        }
        $res=$logic->insert($menu_data);
        if($res){
            return $this->showmessage("添加菜单成功！", "成功信息","?menu/menulist");
        }else{
            return $this->showmessage("添加菜单失败！", "错误信息");
        }
        
    }
}
